package pro.lingwu.station.handler;

import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import pro.lingwu.station.exception.HasValueException;

/**
 * @author @lingwu
 * @date created in 11/19/2021
 */
@ControllerAdvice
public class SqlExceptionHandler {

    /**
     * 捕获账号重复异常
     * @param exception 异常
     * @return 响应体
     */
    @ExceptionHandler({DuplicateKeyException.class})
    @ResponseBody
    public ResponseEntity<?> idRepeatHandler(DuplicateKeyException exception){
        exception.printStackTrace();
        return ResponseEntity.badRequest().body("账号/id 重复");
    }

    /**
     * 捕获同一乘客购买同一车次的次数过多
     */
    @ExceptionHandler({HasValueException.class})
    @ResponseBody
    public ResponseEntity<?> buyTooManyTickets(HasValueException exception){
        exception.printStackTrace();
        return ResponseEntity.badRequest().body("已经购买过此车车票");
    }

}
